/*
 * MIT License
 * 
 * Copyright (c) 2017 wen.gu <454727014@qq.com>
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/***************************************************************************
 * Name: vos_virtual_watchdog.h
 *
 * Purpose: virtual os layer virtual watchdog interface(or macro) 
 *
 * Developer:
 *   wen.gu , 2016-05-09
 *
 * TODO: 
 *
 ***************************************************************************/

#ifndef __VOS_VIRTUAL_WATCHDOG_H__
#define __VOS_VIRTUAL_WATCHDOG_H__

#include <stdio.h>
#include <string.h>

#include "general_type.h"
#include "general_error.h"
#include "general_macro.h"

#include "vos_mutex.h"
#include "vos_sys.h"


#ifdef __cplusplus
extern "C"{
#endif

/***************************************************************************
 *
 * watchdog lite version struct declaration
 *
 ***************************************************************************/
typedef struct vosWatchdogLite_s
{
	GPHD lock; //mutex lock
	GBOL isEnable; //GBOL, true:enable, false: disable
	GS64 baseTime;
	GS64 counter;  //timeout counter
}vosWatchdogLite_t;
 /***************************************************************************
 *
 * watchdog lite version API declaration
 *
 ***************************************************************************/
/**
*@brief  initialize a lite version watchdog
*
*@param  wtd [in] lite version watch data instance.
*
*@return success:G_OK, failed: G_FAIL.
*@see 
*/
static inline GS32 vosWatchdogLiteInit(vosWatchdogLite_t* wtd)
{
	GSTRUCT_PTR_INIT(wtd);
	wtd->isEnable = GFALSE;
	wtd->lock = vosMutexCreate();

	if (!(wtd->lock))
	{
		return G_FAIL;
	}

	return G_OK;
}

/**
*@brief  deinitialize a lite version watchdog
*
*@param  wtd [in] lite version watch data instance.
*
*@return None.
*@see 
*/
#define vosWatchdogLiteDeinit(wtd) do{vosMutexFree((wtd)->lock);}while(0)

/**
*@brief  reset a lite version watchdog
*
*@param  wtd          [in] lite version watch data instance.
*@param  timeOutCntMs [in] timeout counter(unit in ms)
*
*@return None.
*@see 
*/
#define vosWatchdogLiteReset(wtd, timeOutCntMs) \
		do{vosMutexLock((wtd)->lock);        \
			(wtd)->counter = timeOutCntMs;   \
			(wtd)->baseTime = vosSysTimeMs(); /*get now time as base*/  \
			(wtd)->isEnable = GTRUE;        \
			vosMutexUnlock((wtd)->lock);     \
		}while(0)

/**
*@brief  enable a lite version watchdog
*
*@param  wtd [in] lite version watch data instance.
*
*@return None.
*@see 
*/
#define vosWatchdogLiteEnable(wtd) \
		do{ vosMutexLock((wtd)->lock);        \
		    (wtd)->baseTime = vosSysTimeMs(); /*get now time as base*/  \
		    (wtd)->isEnable = GTRUE;        \
		    vosMutexUnlock((wtd)->lock);     \
		}while(0)

/**
*@brief  disable a lite version watchdog
*
*@param  wtd [in] lite version watch data instance.
*
*@return None.
*@see 
*/
#define vosWatchdogLiteDisable(wtd)     \
		do{ vosMutexLock((wtd)->lock);   \
		    (wtd)->isEnable = GFALSE;   \
		    vosMutexUnlock((wtd)->lock); \
		}while(0)



/**
*@brief  get a lite version watchdog is or isn't timeout 
*
*@param  wtd [in] lite version watch data instance.
*
*@return B_TURE:was timeout, GFALSE: wasn't timeout
*@see 
*/
static inline GBOL vosWatchdogLiteIsTimeout(vosWatchdogLite_t* wtd)
{
	GBOL ret = GFALSE;
	vosMutexLock(wtd->lock);
	if (GTRUE == wtd->isEnable)
	{
		GS64 curTime = vosSysTimeMs(); //get now time.

		if (curTime > wtd->baseTime)
		{
			if ((curTime - wtd->baseTime) > wtd->counter)
			{
				ret = GTRUE;
			}
		}
		else //if base time was invalid,fix it.
		{
			//LOGW("dog's base time is invalid(%lld)ms, cur time(%lld)ms\n",
			//	wtd->baseTime, curTime);
			wtd->baseTime = curTime; //get current time as base time
		}
	}
	vosMutexUnlock(wtd->lock);

	return ret;
}

/***************************************************************************
 *
 * watchdog full version struct declaration
 *
 ***************************************************************************/

 /***************************************************************************
 *
 * watchdog full version API declaration
 *
 ***************************************************************************/
#ifdef __cplusplus
}
#endif
#endif //end of __VOS_VIRTUAL_WATCHDOG_H__
